<?php

namespace app\admin\controller\microend;

use app\common\controller\Backend;

use think\Db;

/**
 * 派克身份认证管理
 *
 * @icon fa fa-circle-o
 */
class Auther extends Backend
{
    
    /**
     * Real模型对象
     * @var \app\common\model\auth\Real
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\common\model\auth\Real;
        $this->view->assign("authStatusList", $this->model->getAuthStatusList());
    }
    
    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */

    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = false;
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax())
        {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField'))
            {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                ->where($where)
                ->order($sort, $order)
                ->count();

            $list = $this->model
                ->field("*,auth_id as id")
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();

            $list = collection($list)->toArray();

            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }



    /**
     *  改变状态
     *
     *  type
     *
     *  1 : 通过
     *
     *  2 ： 驳回
     *
     */
    public function update_status(){

        if ($this->request->isPost()) {
            $params = $this->request->post();

            if ($params)
            {
               // halt($params);
                try{
                    $this->model->save(["auth_status"=>$params["type"]],["auth_id"=>$params["row"]["auth_id"]]);
                    if($params["type"]==1){
                        $uid = $params["uid"];
                        $openid = Db::name("wx_users")->where("uid",$uid)->value("openid");
                        $msg = [
                            "condition"=>3,
                            "touser"=>$openid,
                            "content"=>[$params["username"],"身份证审核"]
                        ];
                        $MiniMsg = new  \app\common\lib\MiniMsg($msg);
                        $MiniMsg->sendMsg();//发送短信模板注册成功

                    }elseif($params["type"]==2){
                        $uid = $params["uid"];
                        $openid = Db::name("wx_users")->where("uid",$uid)->value("openid");
                        $msg = [
                            "condition"=>4,
                            "touser"=>$openid,
                            "content"=>[$params["username"],"身份证审核"]
                        ];
                        $MiniMsg = new  \app\common\lib\MiniMsg($msg);
                        $MiniMsg->sendMsg();//发送短信模板注册成功
                    }
                    renderJson("成功:".json_encode($params), 1);
                }catch (\Exception $e){
                    renderJson("失败", 0);
                }

            }
        }

    }


    /**
     * 批量通过
     *

     *
     */
    public function mult_pass(){
        $pass = input("post.");
        $ids = [];
        if($pass["ids"]) {

            foreach ($pass["ids"] as $k => $v) {
                $ids[] = $v["auth_id"];
            }
            Db::name("auth_real")->where("auth_id", "in", $ids)->update(["auth_status" => 1]);
            renderJson("成功通过", 1);
        }else{
            renderJson("请勾选你的选项", 0);
        }

    }


    /**
     *
     * 批量驳回
     */
    public function mult_no(){

        $no = input("post.");
        $ids = [];
        if($no["ids"]) {
            foreach ($no["ids"] as $k => $v) {
                $ids[] = $v["auth_id"];
            }
            Db::name("auth_real")->where("auth_id", "in", $ids)->update(["auth_status" => 2]);
            renderJson("成功驳回", 1);
        }else{

            renderJson("请勾选你的选项", 0);
        }

    }
    

}
